<?php
/**
 * Description of Database
 *
 * @author E.Mylcin
 */

includeClass('SqlProcessor', __DIR__."/classes");

define('DB_COND_NOT_BETWEEN', -5);
define('DB_COND_NOT_IN', -4);
define('DB_COND_NOT_LIKE', -3);
define('DB_COND_LT', -2);
define('DB_COND_LTE', -1);
define('DB_COND_EQUAL', 0);
define('DB_COND_GTE', 1);
define('DB_COND_GT', 2);
define('DB_COND_LIKE', 3);
define('DB_COND_IN', 4);
define('DB_COND_BETWEEN', 5);

define('DB_LOGIC_NOT', -1);
define('DB_LOGIC_AND', 0);
define('DB_LOGIC_OR', 1);

class Database {

    protected $px, $data, $action;

    function __construct($params) {
        $this->px = isset($params['px']) ? $params['px'] : "";
        return $this;
    }
    
    function __destruct() {
        $this->data = false;
        $this->action = false;
    }
    
    function select(){
        $this->data['action'] = __FUNCTION__;
        return $this;
    }
    
    function insert(){
        $this->data['action'] = __FUNCTION__;
        return $this;
    }
    
    function update(){
        $this->data['action'] = __FUNCTION__;
        return $this;
    }
    
    function delete(){
        $this->data['action'] = __FUNCTION__;
        return $this;
    }
    
    function native($query){
        $this->data['action'] = __FUNCTION__;
        $this->data['query'] = preg_replace("/@px:/", $this->px, $query);
        return $this;
    }
    
    function option($str){
        $this->data[__FUNCTION__] = $str;
        return $this;
    }
    
    function table($tables, $alias = false){
        if(is_array($tables)){
            foreach($tables as $table => $as){
                if(is_numeric($table)){
                    $table = $as;
                    $as = false;
                }
                $this->data[__FUNCTION__][] = array('name' => $this->px.$table, 'alias' => $as);
            }
        }else{
            $this->data[__FUNCTION__][] = array('name' => $this->px.$tables, 'alias' => $alias);
        }
        return $this;
    }
    
    function field($tables = "*", $alias = false){
        if(is_array($tables)){
            foreach($tables as $table => $as){
                if(is_numeric($table)){
                    $table = $as;
                    $as = false;
                }
                $this->data[__FUNCTION__][] = array('name' => $table, 'alias' => $as);
            }
        }else{
            $this->data[__FUNCTION__][] = array('name' => $tables, 'alias' => $alias);
        }
        return $this;
    }
    function order($field, $direction){
        $this->data[__FUNCTION__][] = array(
            'field' => $field,
            'direction' => $direction,
        );
        return $this;
    }
    
    function limit($from, $length){
        $this->data[__FUNCTION__] = array(
            'from' => $from,
            'length' => $length,
        );
        return $this;
    }
    
    function values($values){
        $data = func_get_args();
        foreach($data as $value){
            $this->data[__FUNCTION__][] = $value;
        }
        return $this;
    }
    
    function cond($filed, $value, $cond = DB_COND_EQUAL, $logic = DB_LOGIC_AND){
        $this->data[__FUNCTION__][] = array(
            'field' => $filed,
            'value' => $value,
            'cond' => $cond,
            'logic' => $logic,
            'noquotes' => false,
        );
        $this->action = __FUNCTION__;
        return $this;
    }
    
    function set($filed, $value){
        $this->data[__FUNCTION__][] = array(
            'field' => $filed,
            'value' => $value,
            'noquotes' => false,
        );
        $this->action = __FUNCTION__;
        return $this;
    }
    
    function execute(){
        Logger::message($this->getSql());
    }
    
    function noquotes(){
        $this->data[$this->action][count($this->data[$this->action]) - 1]['noquotes'] = true;
        return $this;
    }
    
    function getSql(){
        $str = "@action:";
        $SQLPROC = new SqlProcessor;
        foreach ($this->data as $method => $params){
            $str = preg_replace("/@$method:/si", $SQLPROC->$method($params), $str);
        }        
        $str = preg_replace("/\s@[\S]+:/si", "", $str);
        $SQLPROC = null;
        $this->data = null;
        $this->action = null;
        return $str;
    }
    
    static function Init($params = array()){
        if(empty($params)){
            $params = parse_ini_file(__DIR__."/config.ini");
        }        
        if(isset($params['type']) && $params['type'] != "" && $params['type'] != "Database"){
            $filename = __DIR__."/classes/".$params['type'].".class.php";                        
            if(file_exists($filename)){
                $class = "\\Database\\".$params['type'];
                require_once $filename;
                return new $class($params);
            }else{
                return new Database($params);
            }
        }else{
            return new Database($params);
        }
    }
    
    function GetFields($tablename){
        return array();
    }
}
